import { Injectable } from '@angular/core';
import { LonginInfoDTO } from '../model/loginInfo-dto';
import { Observable } from 'rxjs';
import { JwtTokenDTO } from '../model/jwt-token-dto';
import { _HttpClient } from '@delon/theme';
import { CacheService } from '@delon/cache';
import { UserDTO } from '../model/user-dto';

@Injectable({
  providedIn: 'root',
})
export class LoginService {
  /**
   * API的入口URL
   */
  private static URL = '/api/token';

  private static UserUrl = '/api/authority/users';

  constructor(private http: _HttpClient, private cacheService: CacheService) {}

  /**
   * 获取Token
   */
  login(longinInfoDTO: LonginInfoDTO): Observable<JwtTokenDTO> {
    return this.http.post<JwtTokenDTO>(`${LoginService.URL}?_allow_anonymous=true&encrypt=body`, longinInfoDTO);
  }

  /**
   *
   * 根据ID查找用户
   * @param id 用户id
   */
  findById(id: string): Observable<UserDTO> {
    return this.http.get<UserDTO>(`${LoginService.UserUrl}/${id}`);
  }
}
